package com.tedu._03vehicle.base.filters;

import com.tedu._03vehicle.pojo.vo.UserVO;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;

import java.io.IOException;
@WebFilter(filterName = "loginChckeFilter" , urlPatterns = {"/v1/vehicle/*"})
public class LoginCheckFilter implements Filter {

    @Override

    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化过滤器");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("进入过 滤器");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO != null) {
            filterChain.doFilter(request,response);
        }else {
            // http://localhost:8080/login.html
            // http://localhost:8080/v1/vehicle/select/login.html
             response.sendRedirect("/login.html");
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }


}
